<template>
    <div style="width:550px">
        <el-button type='primary' size='small' @click='diaShow=true'>导入数据</el-button>
        <el-button type='primary' size='small' @click='addpk'>一键注册新字段</el-button>
        <el-button type='primary' size='small' @click='importModel(1)' :disabled="!ready">一键导入</el-button>
        <span>完成度: {{finish}} / {{lines.length-1}}</span>
        <el-table :data="modelData.headerList" :fit='false' style="width:100%;margin-top:20px" size="small">
            <el-table-column type='index' width='50'></el-table-column>
            <el-table-column prop='keyName' label="字段名称" width='300'></el-table-column>
            <el-table-column prop='keyId' label="字段是否注册" width='200'>
                <template slot-scope="scope">
                    <span v-if="scope.row.keyId==''" style="color:red">未注册</span>
                    <span v-else style="color:teal">已注册</span>
                </template>
            </el-table-column>
        </el-table>

        <el-dialog :visible.sync="diaShow" width='600px' center :close-on-click-modal='false'>
            <el-input style="width:550px" :rows="10" type="textarea" resize="none" v-model="textarea" @input='dataCount'></el-input>
            <span>数据条数: {{lineNum}}  字段数量: {{colNum}}  {{rs}} </span>
            <span slot="footer" class="dialog-footer">
                <el-button size='small' @click='diaShow=false'>返回</el-button>
                <el-button size='small' @click='setHeaderList()' type='primary' :disabled="lineNum==0">下一步</el-button>
            </span>
        </el-dialog>
        
    </div>
</template>

<script>
export default {
    name: 'modelTable',
    data(){
        return {
            diaShow:false,
            textarea:'',
            lines:[],
            lineNum:0,
            colNum:0,
            rs:'',
            idIndex:-1,
            ready:false,
            finish:0,
            wordBook:[],
            modelData:{
                headerList:[],
                modelList:[]
            }
        };
    },
    methods:{
        dataCount:function(){
            this.lines=[];
            this.rs="";
            var str=this.textarea.split("\n");
            while(str[str.length-1]=='')str.splice(str.length-1,1);
            var line=str[0].split("\t");
            this.colNum=line.length;
            for(var i=1;i<str.length;i++){
                line=str[i].split("\t");
                if(line.length!=this.colNum){
                    this.rs="数据异常";
                    return;
                }
            }
            this.lineNum=str.length;
            this.rs="检查通过";
            this.lines=str;
        },
        setHeaderList:function(){
            var lines=this.textarea.split("\n");
            var line=lines[0].split("\t");
            var headerList=[];
            var temp='';
            this.ready=true;
            for(var i=0;i<line.length;i++){
                temp=this.getPropertyKey(line[i]);
                if(temp!=null){
                    headerList.push(temp);
                    if(temp.keyId=='id')this.idIndex=i;
                }else{
                    headerList.push({keyId:'',keyName:line[i]});
                    this.ready=false;
                }
            }
            this.modelData.headerList=headerList;
            this.diaShow=false;
        },
        addpk:function(){
            var headerList=this.modelData.headerList;
            for(var i=0;i<headerList.length;i++){
                if(headerList[i].keyId==''){
                    this.addpkAjax(i,headerList[i]);
                }
            }
        },
        addpkAjax:function(index,pk){
            var params=new URLSearchParams();
            params.append('propertyType','TEXT');
            params.append('upid','基本参数');
            params.append('keyName',pk.keyName);
            this.$http.post(this.$store.state.url+'/addpropertykey',params).then(
            response=>{
                if(response.data.status==200){
                    pk.keyId=response.data.data.keyId;
                    this.ready=true;
                }
            }
            ).catch();
        },
        importModel:function(index){
            this.ready=false;
            var headers=this.modelData.headerList;
            var params=new URLSearchParams();
            params.append("idIndex",this.idIndex);
            for(var i=0;i<headers.length;i++){
                params.append('keyIds',headers[i].keyId);
            }
            var end=index+100;
            for(;index<end;index++){
                if(index==this.lines.length)break;
                params.append('lines',this.lines[index]);
            }
            
            this.$http.post(this.$store.state.url+"/modelproperty/savemore",params).then(
                response=>{
                    this.finish=index-1;
                    if(index<this.lines.length)this.importModel(index);
                }
            ).catch(error=>{
                this.ready=true;
            });
            
        },

        wordBookInit:function(){
            this.$http.post(this.$store.state.url+"/modelfilterinit").then(
                response=>{
                    this.wordBook=response.data.data;
                    this.wordBook.unshift({keyId:'id',keyName:'车型Id'});
                }
            ).catch(error=>{});
        },
        getPropertyKey:function(keyName){
            for(var i=0;i<this.wordBook.length;i++){
                if(this.wordBook[i].keyName==keyName)return this.wordBook[i];
            }
            return null;
        }
    },
    components:{
        
    },
    created:function(){
        this.wordBookInit();
    },
    props:[]
}

</script>

<style scoped>

</style>
